package svg;

import java.awt.BorderLayout;
import java.io.File;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import org.apache.batik.dom.svg.SAXSVGDocumentFactory;
import org.apache.batik.swing.JSVGCanvas;
import org.apache.batik.util.XMLResourceDescriptor;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.events.EventTarget;

import util.EventType;
import util.IDs;
import util.RetryListener;
import util.StarListener;

/**
 * This class represents the UI of the game.
 */
public class MainWindow extends JFrame
{

	private static final long serialVersionUID = -1191869306254273975L;
	private JPanel contentPane;
	private Document _boardDoc;
	private BoardAdapter _boardAdapter;
	private GameController _controller;
	private JSVGCanvas _boardCanvas;
	private StarListener _starListener;
	private RetryListener _retryListener;

	/**
	 * Create the frame, loads the board and registers the listeners.
	 */
	public MainWindow() {
		setTitle("SunBeam");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 682, 535);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		loadBoard();
		
		contentPane.add(_boardCanvas, BorderLayout.CENTER);
		
		setVisible(true);
	}

	/**
	 * This method registers the click listeners.
	 */
	public void registerListener() {
		_starListener = new StarListener(_controller, _boardAdapter);
		_retryListener = new RetryListener(_controller, _boardAdapter);
		
		// listener for stars
    	for (int i = 1; i < 20; i++)
    	{
    		Element star = _boardCanvas.getSVGDocument().getElementById("star" + i);
    		EventTarget target = (EventTarget) star;
    		target.addEventListener(EventType.CLICK.toString(), _starListener, false);
    		target.addEventListener(EventType.MOUSE_OVER.toString(), _starListener, false);
    		target.addEventListener(EventType.MOUSE_OUT.toString(), _starListener, false);
    		target.addEventListener(EventType.MOUSE_DOWN.toString(), _starListener, false);
    		target.addEventListener(EventType.MOUSE_UP.toString(), _starListener, false);
    	}
    	
    	// click listener for restart game
    	Element text = _boardCanvas.getSVGDocument().getElementById(IDs.RETRY.toString());
    	EventTarget target = (EventTarget) text;
    	target.addEventListener(EventType.CLICK.toString(), _retryListener, false);
    	
	}

	/**
	 * This method loads the board from the SVG file and parses it to a document.
	 * It also creates the board adapter which can manipulate the board.
	 * It also creates the SVG canvas which can be displayed in a JFrame.
	 */
	private void loadBoard() {
		// load svg
		String parser = XMLResourceDescriptor.getXMLParserClassName();
        SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
		String uri = new File(".\\res\\Board.svg").toURI().toString();
		
		// create document
		try {
			_boardDoc = f.createDocument(uri);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		// create board adapter
		_boardAdapter = new BoardAdapter(_boardDoc);

		// add document to svgCanvas
		_boardCanvas = new JSVGCanvas();
		_boardCanvas.setDocumentState(JSVGCanvas.ALWAYS_DYNAMIC);
		_boardCanvas.setDocument(_boardDoc);
	}

	/**
	 * Sets a reference to the game controller.
	 * 
	 * @param gameController the game controller
	 */
	public void setGameController(GameController gameController) {
		_controller = gameController;
	}

	/**
	 * Returns the board adapter.
	 * @return
	 */
	public BoardAdapter getBoardAdapter() {
		return _boardAdapter;
	}
}
